﻿app.controller('UserDetailController', function ($scope, $rootScope, $routeParams, userFactory, notificationFactory, fileFactory, base64Factory) {

    init();
    var imgData;
    //all implement data model call in a init() function

    function init() {
        $scope.routeId = $routeParams.routeid;
        $scope.fileSever = mainFileAddress;
        $scope.roleByGroup = JSON.parse(localStorage.getItem("menu"));
        $scope.userName = $rootScope.userName;
        //count--------------
        notificationFactory.countAddFriend(localStorage.getItem("idkey")).success(function (data) {
            $scope.countAddFriend = data;
        });
        notificationFactory.countFromRoom(localStorage.getItem("idkey")).success(function (data) {
            $scope.countFromRoom = data;
        });
        notificationFactory.countNewMessage(localStorage.getItem("idkey")).success(function (data) {
            $scope.countNewMessage = data;
        });

        //--
        $scope.changePassStatus = false;
        $scope.changePassShow = false;
        $scope.changeAvatarShow = false;

        $scope.passwordData = { UserId: localStorage.getItem("idkey"), PasswordOld: null, Password: null, PasswordC: null };
        $scope.formData = { Id: null, UserName: null, FullName: null, Email: null, PhoneNumber: null, Status: null, Block: null, GroupId: null, Avatar: null };
        if ($routeParams.UserId == null)
            $scope.getUserData = userFactory.getUser(localStorage.getItem("idkey")).success(function (data) {
                $scope.formData.Id = data.Id;
                $scope.formData.Email = data.Email;
                $scope.formData.UserName = data.UserName;
                $scope.formData.FullName = data.FullName;
                $scope.formData.PhoneNumber = data.PhoneNumber;
                $scope.formData.Block = data.Block;
                $scope.formData.GroupId = data.GroupId;
                $scope.formData.Avatar = data.Avatar;
            });
        else {
            $scope.getUserData = userFactory.getUser($routeParams.UserId).success(function (data) {
                $scope.formData.Id = data.Id;
                $scope.formData.Email = data.Email;
                $scope.formData.UserName = data.UserName;
                $scope.formData.FullName = data.FullName;
                $scope.formData.PhoneNumber = data.PhoneNumber;
                $scope.formData.Block = data.Block;
                $scope.formData.GroupId = data.GroupId;
                $scope.formData.Avatar = data.Avatar;
            });
        }

        $scope.updateUser = function () {
            //alert("vao day ko updateuser");
            $scope.pageLoading = true;
            if ($scope.changeAvatarShow == true) {
                if (imgData != null) {
                    fileFactory.uploadFile({ Text: imgData, Path: "Avatar" }).success(function (data) {
                        console.log(data.Text);
                        $scope.formData.Avatar = data.Text;
                        userFactory.putUser($scope.formData).success(function () {
                            $scope.pageLoading = false;
                            $scope.errorStatus = false;
                            $scope.successStatus = true;
                        }).error(function () {
                            $scope.pageLoading = false;
                            $scope.successStatus = false;
                            $scope.errorStatus = true;
                        });
                    });
                }
            }
            if ($scope.changePassShow == true) {
                userFactory.changePassword($scope.passwordData).success(function (edata) {
                    var authen = 'Basic ' + base64Factory.encode($scope.formData.UserName + ':' + $scope.passwordData.Password);
                    localStorage.setItem("authen", authen);
                    $scope.pageLoading = false;
                }).error(function () {
                    $scope.pageLoading = false;
                    //$("#updateStatus").append("Lỗi quá trình cập nhật!");
                });
            }
            if ($scope.changePassShow == false && $scope.changeAvatarShow == false) {
                userFactory.putUser($scope.formData).success(function () {
                    $scope.pageLoading = false;
                    $scope.errorStatus = false;
                    $scope.successStatus = true;
                }).error(function () {
                    $scope.pageLoading = false;
                    $scope.successStatus = false;
                    $scope.errorStatus = true;
                });
            }
        };


        $scope.changePass = function () {
            //console.log($("#password").attr("required"));
            if ($scope.changePassShow == false) {
                $("#passwordold").attr('required');
                $("#password").attr('required');
                $("#password_c").attr('required');
            } else {
                $("#passwordold").removeAttr('required');
                $("#password").removeAttr('required');
                $("#password_c").removeAttr('required');
            }
            $scope.changePassShow = !$scope.changePassShow;
        };
        $scope.changeAvatar = function () {

            $scope.changeAvatarShow = !$scope.changeAvatarShow;
        };
        //đính kèm
        $scope.selectPicture = function () {
            navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
                quality: 50,
                destinationType: destinationType.DATA_URL,
                sourceType: pictureSource.PHOTOLIBRARY
            });
        };
        $scope.takePicture = function () {
            navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
                quality: 50,
                destinationType: destinationType.DATA_URL
            });
        };



        createMenu();
    }
    function onFail() { }

    function onPhotoDataSuccess(imageData) {
        imgData = imageData;
        var smallImage = document.getElementById('newimg');
        smallImage.style.display = 'block';
        smallImage.src = "data:image/jpeg;base64," + imageData;
    }
});